Ext.define('PJ.view.celaquestionnairemanage.SelectQuestionsList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.celaSelectQuestionsList'],
    requires: ['AM.default.DefaultSearchForm'],
    
    initComponent: function(){
    	var me = this;
        me.columns = [{
            header: '题目题干',
            dataIndex: 't.questionName',
            flex: 1,
            minWidth: 120,
			renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '题目题型',
            dataIndex: 't.questionType',
            renderer: function(value){
                return value;
            },
            width: 130
        },{
            menuDisabled: true,
            sortable: false,
            // 禁止调整列宽
            resizable: false,
            hideable: false,
            xtype: 'actioncolumn',
            header: '操作',
            width: 80,
            items: [{
                tooltip: '查看详细信息',
                getClass: function(v, meta, rec){
                    return "browseIcon";
                },
                handler: function(grid, rowIndex, colIndex, item, e, record){
                	Ext.create('PJ.view.celaquestionnairemanage.SetQuestionsView', {
                        title: "查看题目详细信息",
                        grid: me,
                        isEdit: false,
                        notEdit: false,
                        questionId: record.get("questionId")
                    }).show();
                }
            }]
        }];
        me.store = Ext.create('PJ.'+ prototype +'.celaquestionnairemanage.SelectQuestionsList', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        me.callParent(arguments);
    },
    
    //新增
    onSelectClick: function(button){
    	var me = this;
        var records = me.getSelectionModel().getSelection();
        if(gj_onlineLearning_constant.loadType == "jsonp"){
        	Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要添加这些题目吗?',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
                    	var questionIds = new Array();
                        Ext.Array.each(records, function(name, index){
                        	questionIds.push(name.get("questionId"));
                        });
                        var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
        					loadType: gj_onlineLearning_constant.loadType,
        					url: me.store.addUrl,
        					extraParams: {
        						questionnaireId:me.up("window").questionnaireId,
        						questionIds: questionIds,
        						questionType:me.up("window").questionType
        					}
        				});
        				store.load(function(rec, operation, success){
        					if(success){
        						Orchid.alert('消息', '添加成功！', function(){
                                    me.up("window").grid.getStore().load();
                                    me.up('window').close();
                                });
        					}else{
        						Orchid.alert('消息', operation.error);
        					}
        				});
                    }
                    else {
                        return;
                    }
                }
            });
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
        	Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要添加这些题目吗？',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
                        var questionIds = new Array();
                        Ext.Array.each(records, function(name, index){
                        	questionIds.push(name.get("questionId"));
                        });
                        Ext.Ajax.request({
                            url: me.store.addUrl,
                            params: {
                            	questionnaireId:me.up("window").questionnaireId,
        						questionIds: questionIds,
        						questionType:me.up("window").questionType
                            },
                            success: function(response){
                                var data = response.responseText;
                                var json = Ext.decode(data, true);
                                if (json.success) {
                                	Orchid.alert('消息', '添加成功！', function(){
                                        me.up("window").grid.getStore().load();
                                        me.up('window').close();
                                    });
                                }
                                else {
                                    Orchid.alert('消息', json.msg, function(){
                                        me.up("window").grid.getStore().load();
                                        me.up('window').close();
                                    });
                                }
                            }
                        });
                    }
                    else {
                        return;
                    }
                }
            });
        }
    },
    
    listeners: {
    	selectionchange: function(selModel, selected){
			this.down('#' + this.oldId + "Select").setDisabled(selected.length === 0);//发布
		},
    	viewready: function(){
    		this.addDocked([{
    			dock: 'top',
    			xtype: "searchform",
    			columnCount: 2,
    			fieldDefaults: {
    				labelAlign: 'right',
    				labelWidth: 100
    			},
    			items: [{
    				xtype: "textfield",
    				fieldLabel: '题目题干',
    				name: "questionName",
    				maxLength: "100",
    				emptyText: "请输入题目题干"
    			}],
                onResetClick: function(button){
                    var form = button.up('form');
                    var gridPanel = form.grid;
                    var newParams = {
                    		questionnaireId:this.up("window").questionnaireId,
                    		questionType:this.up("window").questionType
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    // 4.0强制更改store参数写法
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    form.getForm().reset();
                },
    			grid: this
    		}, {
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
                	text: '选取',
					iconCls: 'acceptIco',
					itemId: this.oldId + "Select",
					disabled: true,
					scope: this,
					handler: this.onSelectClick
                }, "->", this.countSelectPanel]
            }]);
    		var store = this.store;
            var newParams = {
            		questionnaireId:this.up("window").questionnaireId,
            		questionType:this.up("window").questionType
            };
            store.proxy.extraParams = {};
            // 4.0强制更改store参数写法
            var params = store.proxy.extraParams;
            Ext.apply(params, newParams);
            store.isLoad=true;
            store.load();
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    	},
    	afterrender: function(){
    		this.loadMask = new Ext.LoadMask(this, {
                msg: "正在加载中,请稍后...."
            });
    	}
    }
});
